package dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

import Utils.DataSourceUtils;
import domain.User;

public class CheckUsernameByAjaxDao {

	/*
	 * 通过username获取user
	 */
	public User cherkuserByUsername(String username) throws SQLException {
		QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
		
		String sql="select * from user where username=? limit 1";
		
		return qr.query(sql, new BeanHandler(User.class),username);
	}

	/**
	 * 模仿百度模糊查询
	 * @param kw
	 * @return
	 * @throws SQLException 
	 */
	public List searchkw(String kw) throws SQLException {
		QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
		
		String sql="select kw from keyword where kw like ? limit 5";
		
		return qr.query(sql, new ColumnListHandler("kw"),"%"+kw+"%");   //ColumnListHandler将结果集的某一列存放在list中
	}

}
